#ifndef SORTCG_H
#define SORTCG_H

#include "udefcg.h"


typedef struct swpdata {
	ino_t		ino;		/* cislo inodu */
	uint32_t	apindx;		/* index do pole bloku */

/* index do db/ib (relativne ke kazdemu bloku od 0 ... n) XXX */	
	uint32_t	blkindx;	
	
/* 
 * pokud je db, buff na oblast inodu pokud ib tak indir block 
 */	
	void		*buf;		
/* 
 * pokud ib, tak adresa ib bloku
 * pokud ZERODESTINO, tak adresa volneho mista
 */
	uint32_t	addr;		
	uint8_t		flags;	
} SWPDATA;


/*
 * obsahuje adresu volneho bloku, kam se v mezikroce ulozi blok 
 */
typedef struct onefreeblk {
	uint32_t	addr;
	uint32_t	cyl;
} OFBLK;


#define			ZERODESTINO			0x1
#define			SWPDB				0x2
#define			SWPIB				0x4

#define			NOMOREBLKS			0x1
#define			OUTOFRANGE			0x2

/* hodnoty pro swap_inode a swap block */
#define			SWAP_ERR 			0
#define			SWAP_OK				1

/* pro swap_src_p */
#define			SWPSRCERR	 		0
#define			SWPSRCOK	 		1

/* pro swap_dest_p */
#define			SWPDESTOK 			0
#define			SWPDESTERR 			1
#define			SWPDESTONPLACE 		2
#define			SWPDESTEMPTY		3
#define			SWPDESTCONT			4


int sortcg(GLBVAL *);
int find_freeblk(GLBVAL *);
int find_freeblk_cg (GLBVAL *, int);
int prepare_swp_src(GLBVAL *, SWPDATA *, int);
int prepare_swp_dest(GLBVAL *, SWPDATA *, SWPDATA *, BLOCKARRS *);
int recount_blkindx(int);
int prepare_swp(GLBVAL *, ino_t, int, SWPDATA *);
int cgbitmap_set(GLBVAL *, uint);
int cgbitmap_clr(GLBVAL *, uint);
int get_dest_blk(GLBVAL *, SWPDATA *);
uint swap_block (GLBVAL *, SWPDATA *, uint);
int swap_inode (GLBVAL *, SWPDATA *, uint);
uint swap_src_p1(GLBVAL *, SWPDATA *);
int swap_dest_p1(GLBVAL *, SWPDATA *, BLOCKARRS **, int);
uint swap_src_p2(GLBVAL *, SWPDATA *, BLOCKARRS *);
int swap_dest_p2(GLBVAL *, SWPDATA *, int);
void bind_swpdata_mem (SWPDATA *, SWPDATA *, int *, int *);
int hop_sort(GLBVAL *, int);
int triple_sort(GLBVAL *, int, int);


#endif
